"""
Configuration resources for FastMCP
"""

from fastmcp import FastMCP, Context
from config import settings
import json


class ConfigResources:
    """Configuration resources for FastMCP"""

    def __init__(self, mcp: FastMCP):
        """Initialize and register configuration resources with the FastMCP server"""
        self.mcp = mcp

    def register(self):
        """Register all configuration-related resources"""
        self.mcp.resource(
            uri="config://app",
            name="Application Configuration",
            description="Current application configuration settings",
            mime_type="application/json",
            tags={"config", "system"},
        )(self.get_app_config)

        self.mcp.resource(
            uri="config://environment",
            name="Environment Information",
            description="Current environment and runtime information",
            mime_type="application/json",
            tags={"config", "environment", "system"},
        )(self.get_environment_info)

        self.mcp.resource(
            uri="config://features",
            name="Feature Flags",
            description="Available features and their status",
            mime_type="application/json",
            tags={"config", "features"},
        )(self.get_feature_flags)

        self.mcp.resource(
            uri="config://limits",
            name="System Limits",
            description="Current system limits and constraints",
            mime_type="application/json",
            tags={"config", "limits", "system"},
        )(self.get_system_limits)

    def get_app_config(self) -> dict:
        """Get the current application configuration."""
        return {
            "app_name": "FastMCP Demo",
            "version": "1.0.0",
            "debug": settings.DEBUG,
            "host": settings.HOST,
            "port": settings.PORT,
            "mcp_host": settings.MCP_HOST,
            "mcp_port": settings.MCP_PORT,
            "mcp_name": settings.MCP_NAME,
        }

    async def get_environment_info(self, ctx: Context) -> dict:
        """Get environment and runtime information."""
        import sys
        import platform
        import os

        await ctx.info("Gathering environment information")

        return {
            "python_version": sys.version,
            "platform": platform.platform(),
            "system": platform.system(),
            "machine": platform.machine(),
            "processor": platform.processor(),
            "environment_variables": {
                key: value
                for key, value in os.environ.items()
                if not key.startswith(("SECRET", "PASSWORD", "TOKEN", "KEY"))
            },
            "working_directory": os.getcwd(),
        }

    def get_feature_flags(self) -> dict:
        """Get available features and their status."""
        return {
            "features": {
                "math_tools": True,
                "text_tools": True,
                "file_tools": True,
                "weather_tools": True,
                "user_resources": True,
                "system_monitoring": True,
                "api_endpoints": True,
                "mcp_server": True,
            },
            "experimental_features": {
                "advanced_analytics": False,
                "machine_learning": False,
                "external_apis": False,
            },
            "integrations": {"fastapi": True, "fastmcp": True, "uvicorn": True},
        }

    def get_system_limits(self) -> dict:
        """Get current system limits and constraints."""
        return {
            "file_operations": {
                "max_file_size": "10KB",
                "max_read_size": 10000,
                "allowed_extensions": [".txt", ".json", ".csv", ".md"],
                "restricted_paths": ["/etc", "/sys", "/proc"],
            },
            "text_processing": {"max_text_length": 50000, "max_replacements": 1000},
            "math_operations": {"max_fibonacci_terms": 100, "max_array_size": 10000},
            "weather": {
                "available_cities": list(
                    ["london", "paris", "tokyo", "newyork", "sydney"]
                ),
                "forecast_days": 5,
            },
        }
